package com.yonyou.crm.bpub.approval.service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.stereotype.Service;

import com.yonyou.crm.bpub.approval.entity.ProcessInstanceVO;
import com.yonyou.crm.bpub.approval.entity.ProcessModelDO;
import com.yonyou.crm.bpub.approval.entity.ProcessModelVO;
import com.yonyou.crm.common.biz.service.BaseBizServiceImpl;
import com.yonyou.crm.common.model.util.ModelUtil;
import com.yonyou.iuap.context.InvocationInfoProxy;

@Service
public class ProcessModelBizServiceImpl extends BaseBizServiceImpl<ProcessModelDO> {

	public ProcessModelVO selectByYsModelKey(String ysModelKey) {
		Map<String, Object> param = new HashMap<String, Object>();
		param.put("tenantId", Long.parseLong(InvocationInfoProxy.getTenantid()));
		param.put("ysModelKey", ysModelKey);
		
		Object object = dao.selectOne(ProcessModelDO.class, "selectByYsModelKey", param);
		ProcessModelDO processModelDO = null;
		if (object != null) {
			processModelDO = (ProcessModelDO)object;
		}
		ProcessModelVO processModelVO = ModelUtil.copyModel(processModelDO, ProcessModelVO.class);
		
		return processModelVO;
	}
	
	public ProcessModelVO selectByYsModelId(String ysModelId) {
		Map<String, Object> param = new HashMap<String, Object>();
		param.put("tenantId", Long.parseLong(InvocationInfoProxy.getTenantid()));
		param.put("ysModelId", ysModelId);
		
		Object object = dao.selectOne(ProcessModelDO.class, "selectByYsModelId", param);
		ProcessModelDO processModelDO = null;
		if (object != null) {
			processModelDO = (ProcessModelDO)object;
		}
		ProcessModelVO processModelVO = ModelUtil.copyModel(processModelDO, ProcessModelVO.class);
		
		return processModelVO;
	}
	
	public ProcessModelVO selectByType(Long bizTypeId, Integer djType) {
		Map<String, Object> param = new HashMap<String, Object>();
		param.put("tenantId", Long.parseLong(InvocationInfoProxy.getTenantid()));
		param.put("bizTypeId", bizTypeId);
		param.put("djType", djType);
		
		Object object = dao.selectOne(ProcessModelDO.class, "selectByType", param);
		ProcessModelDO processModelDO = null;
		if (object != null) {
			processModelDO = (ProcessModelDO)object;
		}
		ProcessModelVO processModelVO = ModelUtil.copyModel(processModelDO, ProcessModelVO.class);
		
		return processModelVO;
	}
	
	public List<ProcessModelVO> getProcessModelList(Map<String, Object> paraMap) {
		if (paraMap == null) {
			paraMap = new HashMap<String, Object>();
		}
		
		paraMap.put("tenantId", Long.parseLong(InvocationInfoProxy.getTenantid()));
		
		List<ProcessModelDO> doList = dao.selectVoList(ProcessModelDO.class, "getProcessModelList", paraMap);
		List<ProcessModelVO> list = ModelUtil.copyModelList(doList, ProcessModelVO.class);
		return list;
	}
	
	public int deleteByModelId(String modelId) {
		Map<String, Object> param = new HashMap<String, Object>();
		param.put("tenantId", Long.parseLong(InvocationInfoProxy.getTenantid()));
		param.put("ysModelId", modelId.trim());
		return dao.deleteSql(ProcessModelDO.class, "deleteByModelId", param);
	}
}
